package ScalaStudy.chapter06

object test12_Singleton {
  def main(args: Array[String]): Unit = {


    /////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////   单例设计模式  /////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////


    val student1 = Student12.getInstance()
    student1.printInfo()

    val student2 = Student12.getInstance()
    student2.printInfo()



  }
}

class Student12 private(val name: String, val age: Int){                     //private
  def printInfo(): Unit ={
    println(s"student46 name = ${name}, age = ${age}, school = ${Student11.school}")
  }
}

//恶汉方式单例模式
//object Student12{
//  private val student: Student12 = new Student12("wq", 27)        //将实例化对象定义成属性
//  def getInstance(): Student12 = student                                       //此方法返回实例化对象
//}

//懒汉方式单例模式
object Student12{
  private var student : Student12 = _
  def getInstance(): Student12 = {
    if(student == null){
      //没有实例对象就创建一个
      student = new Student12("wq", 27)
    }
    student
  }
}
